/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 * 
 * 城市区域 三级联动
 */
$(document).ready(function () {
    var state_data = 'state_id';
    var city_data = 'city_id';
    var area_data = 'area_id';
    loadStates(state_data, city_data, area_data);
    $("#state_id").change(function () {
        state(state_data, city_data, area_data);
    });
    $("#city_id").change(function () {
        city(city_data, area_data);
    });

});

//省份
function loadStates(stateid, city_id, area_id) {
    var state_id = $("#state_id").val();
    $("#" + city_id).attr("disabled", true);
    $("#" + area_id).attr("disabled", true);
    jQuery.ajax({
        type: "get",
        url: "index.php?m=city&a=selCity_ajax&city=3",
        cache: false,
        success: function (result) {
            $("#" + stateid).empty();
            $("#" + stateid).append(result.data);
            if (state_id != null && state_id != "" && state_id != undefined) {
                $("#" + stateid + " option[value='" + state_id + "']").attr("selected", true);
            }
        }
    });
}
function state(stateid, city_id, area_id, state_idTip) {
    var state_id = $("#" + stateid).val();
    if (state_idTip == null || state_idTip == "") {
        state_idTip = "state_idTip";
    }
    if (state_id != null && state_id != "" && state_id != undefined) {
        jQuery.ajax({
            type: "get",
            url: "index.php?m=city&a=selCity_ajax&city=1&stateId=" + state_id,
            cache: false,
            success: function (result) {
                $("#" + city_id).empty();
                $("#" + area_id).empty();
                $("#" + area_id).append("<option value=''>--请选择--</option>");
                $("#" + city_id).append(result.data);
                $("#" + city_id).attr("disabled", false);
                $("#" + area_id).attr("disabled", false);
            }
        });
    } else {
        $("#" + state_idTip).val("省份不能为空");
    }
}
function city(city_id, area_id, city_idTip) {
    var state_id = $("#" + city_id).val();
    if (city_idTip === null || city_idTip === "") {
        city_idTip = "city_idTip";
    }
    if (state_id != null && state_id != "" && state_id != undefined) {
        jQuery.ajax({
            type: "get",
            url: "index.php?m=city&a=selCity_ajax&city=2&stateId=" + state_id,
            cache: false,
            success: function (result) {
                $("#" + area_id).empty();
                $("#" + area_id).append(result.data);
            }
        });
    } else {
        $("#" + city_idTip).val("城市不能为空");
    }
}

